package com.hctg.forced2sleep.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;

import com.actor.myandroidframework.utils.ConfigUtils;
import com.actor.myandroidframework.utils.LogUtils;

/**
 * description: 键盘锁帮助类
 * <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
 *
 * @author : ldf
 * @date   : 2024/5/12 on 12
 * @version 1.0
 */
public class KeyguardManagerUtils {

    private static final KeyguardManager km = (KeyguardManager) ConfigUtils.APPLICATION.getSystemService(Context.KEYGUARD_SERVICE);

    /**
     * newKeyguardLock  可以通过KeyguardLock显示锁屏和隐藏锁屏
     * KeyguardLock方法：
     *
     *       disableKeyguard       禁用锁屏功能
     *
     *       reenableKeyguard    恢复锁屏功能
     *
     *       权限：Manifest.permission.DISABLE_KEYGUARD
     *
     * isKeyguardLocked  返回点前是否处于锁屏的状态   true:锁屏   false:已解锁
     * isKeyguardSecure  是否设置了锁屏密码        true:有锁屏密码（包含SIM卡锁） false:未设置
     * inKeyguardRestrictedInputMode 是否处于限制输入模式  true:不响应home键和右软件 false:功能正常
     * isDeviceLocked  设备是否锁定，功能类似 isKeyguardLocked  多用户接口
     * isDeviceSecure  功能类似isKeyguardSecure 多用户接口
     * requestDismissKeyguard 解锁接口   若设置了锁屏密码需要用户输入正确的密码，若未设置则可以直接解锁
     * exitKeyguardSecurely 解锁接口（无论是否设置了锁屏密码都可以解锁需要获取Manifest.permission.DISABLE_KEYGUARD权限）
     */

    /**
     * 返回点前是否处于锁屏的状态
     * @return true:锁屏   false:已解锁
     */
    public static boolean isKeyguardLocked() {
        return km.isKeyguardLocked();
    }

    /**
     * 返回是否设置了锁屏密码
     * @return true:有锁屏密码（包含SIM卡锁） false:未设置
     */
    public static boolean isKeyguardSecure() {
        return km.isKeyguardSecure();
    }

    /**
     * 返回是否设置了锁屏密码
     * @return true:有锁屏密码（包含SIM卡锁） false:未设置
     */
    @RequiresPermission(Manifest.permission.DISABLE_KEYGUARD)
    public static void disableKeyguard(Activity activity, @Nullable KeyguardManager.KeyguardDismissCallback callback) {
        //if >= Android 8.0(Api 26)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && false) {
            km.requestDismissKeyguard(activity, callback);
        } else {
            if (true) {
                try {
                    PowerManager pm = ((PowerManager) ConfigUtils.APPLICATION.getSystemService(Context.POWER_SERVICE));
//                    PowerManager.WakeLock newWakeLock = pm.newWakeLock(268435466, "bright");
                    @SuppressLint("InvalidWakeLockTag")
                    PowerManager.WakeLock newWakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
                    newWakeLock.acquire(600_000L);   //600s(10min)亮屏
                    KeyguardManager.KeyguardLock unLock = km.newKeyguardLock("unLock");
                    unLock.disableKeyguard();
                    newWakeLock.release();
                    LogUtils.error("正常结束!");
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                try {
//                    PowerManager.WakeLock newWakeLock = ((PowerManager) ConfigUtils.APPLICATION.getSystemService("power")).newWakeLock(268435466, "bright");
//                    newWakeLock.acquire(600000L);
//                    ((KeyguardManager) ConfigUtils.APPLICATION.getSystemService("keyguard")).newKeyguardLock("unLock").disableKeyguard();
//                    newWakeLock.release();
//                    LogUtils.error("正常结束!");
//                } catch (Exception e2) {
//                    e2.printStackTrace();
//                }
            } else {
//            KeyguardManager.KeyguardLock lock = new KeyguardManager.KeyguardLock("");
//            km.exitKeyguardSecurely();

                KeyguardManager.KeyguardLock keyguardLock = km.newKeyguardLock(Context.KEYGUARD_SERVICE);

                keyguardLock.disableKeyguard();
//            newWakeLock.release();

                int flagShowWhenLocked = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;

//                this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//亮屏
//                this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//灭屏

            }
        }
    }

}
